package com.toogu.mazhangmen.ui.monitor;

import android.app.Activity;

import com.google.gson.Gson;
import com.toogu.mazhangmen.bean.EquipmentsBean;
import com.toogu.mazhangmen.ui.main.MainContract;
import com.toogu.mazhangmen.ui.view.MonitorView;
import com.toogu.mazhangmen.util.MonitorUtil;
import com.toogu.mazhangmen.util.OkhttpUtil;
import com.toogu.mazhangmen.util.ToastUtil;

import java.util.List;

public class MonitorPresenter implements MonitorContract.IPresenter {
    private MonitorContract.IView monitorView = null;
    private MonitorContract.IModel monitorModel = null;
    private Activity activity = null;
    private EquipmentsBean equipmentsBean;
    private MonitorUtil monitorUtil;
    private String projectId;

    public MonitorPresenter(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void onCreate(MonitorContract.IView view, MonitorContract.IModel model) {
        equipmentsBean = new Gson().fromJson(activity.getIntent().getStringExtra("equipmentsBean"), EquipmentsBean.class);
        projectId = equipmentsBean.getProject_id();

        this.monitorView = view;
        this.monitorModel = model;
        view.setViewDelegate(IViewDelegate);
        model.setModelDelegate(IModelDelegate);
        view.onCreate();
        model.onCreate();

        view.showEquipment(equipmentsBean);
        monitorModel.getProjectList(projectId);
    }

    @Override
    public void onResume() {
        ToastUtil.getToastUtil().show(activity);//开启消息提醒
        monitorUtil.reStartMonitor();
        monitorView.onResume();
        monitorModel.onResume();
    }

    @Override
    public void onPause() {
        ToastUtil.getToastUtil().close();//关闭消息提醒
        monitorUtil.stopMonitor();
        monitorView.onPause();
        monitorModel.onPause();
    }

    @Override
    public void onDestory() {
        monitorView.onDestory();
        monitorModel.onDestory();
        OkhttpUtil.getOkhttpUtil().cancelTag(activity);
        monitorUtil.destroyMonitor();
        monitorUtil=null;
        activity.finish();
    }

    private MonitorContract.IModel.Delegate IModelDelegate = new MonitorContract.IModel.Delegate() {
        @Override
        public void onDataChanged(List<EquipmentsBean> equipmentsBeanList) {
            monitorView.showDate(equipmentsBeanList);
        }

        @Override
        public void onSetTitle(String title) {
            monitorView.setTitle(title);
        }
    };

    private MonitorContract.IView.Delegate IViewDelegate = new MonitorContract.IView.Delegate() {

        @Override
        public MonitorUtil setMonitorUtil(EquipmentsBean equipmentsBean, MonitorView monitorView) {
            if(monitorUtil!=null) monitorUtil.destroyMonitor();
            monitorUtil=new MonitorUtil(activity,equipmentsBean,monitorView);
            return monitorUtil;
        }

        @Override
        public void getNewDate(String projectId) {
            monitorModel.getProjectList(projectId);
        }

        @Override
        public MonitorAdapter.OnChoiceChange getOnChoiceChange() {
            return new MonitorAdapter.OnChoiceChange() {
                @Override
                public void change(EquipmentsBean equipmentsBean) {
                    monitorView.showEquipment(equipmentsBean);
                }
            };
        }
    };


}
